<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 第三方快捷绑定模块控制器
 */
class Third extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('users_model');
        $this->load->model('account_model');
        $this->load->model('new_task_model');
        $this->load->model('users_spread_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
        $this->load->model('users_log_model');
        $this->load->library('Sms');
        $this->load->library('Email');
    }
    
    //QQ快捷登录
    public function qq_login(){
        $qc = new QC();
        $qc->qq_login();
    }
    //WEIBO快捷登录
    public function weibo_login(){
        session_start();
        $wc = new SaeTOAuthV2(WB_AKEY,WB_SKEY);
        $redirect_url = $wc->getAuthorizeURL(WB_CALLBACK_URL);
        header("Location:".$redirect_url);
    }
    //新账户快捷登录绑定
    function new_bind(){
        if($this->userinfo){
            redirect('user');
        }
        //获取新手任务配置
        $this->new_task = $this->config_task['new_task'];
        $mess = array();
        if($_POST){
            $username = trim($_POST['regname']);//用户名
            $password = trim($_POST['regpass']);//密码
            $qc_info = json_decode(base64_decode(trim($_POST['qc_uinfo'])),true);
            $type = trim($_POST['type']);//快捷登录类型
            $ip = $this->input->ip_address();
            //验证码用户名是否为空
            if(!$username){
                $mess['info'] = '用户名不能为空';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            //验证码用户名的唯一性
            if($this->users_model->get_user_flag_by_username($username)){
                $mess['info'] = '用户名已经被注册';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            //验证密码
            if(!$password){
                $mess['info'] = '密码不能为空';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            /*
            //验证验证码
            if(!$uchptcha){
                $mess['info'] = '验证码不能为空';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            //验证验证码
            if($this->session->userdata('auth_code') != strtolower($uchptcha)){
                $mess['info'] = '验证码错误';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            */
            $parent_id = $this->session->userdata('parent_id') ? $this->session->userdata('parent_id') : 0;
            //生成salt
            $salt = random_string('alnum', 6);
            //生成加密密码
            $md5pass = md5(md5($password) . $salt);
            //写入数据库
            $data = array();
            $data['username'] = $username;
            $data['password'] = $md5pass;
            $data['parent_id'] = $parent_id;
            $data['mobile'] = '';
            $data['email'] = '';
            $data['salt'] = $salt;
            $data['reg_ip'] = $this->input->ip_address();
            $data['reg_time'] = time();
            $data['login_times'] = 1;
            $data['login_ip'] = $this->input->ip_address();
            $data['login_time'] = time();
            $data['last_ip'] = $this->input->ip_address();
            $data['last_time'] = time();
            //创建用户注册信息
            $user_id = $this->users_model->insert($data);
            $month = date('Ym');
            $day = date('Ymd');
            if($user_id){
                
                //绑定快捷登录信息
                $bind_data = array();
                
                if($type == 1){//qq快捷绑定登录
                    $bind_data['user_id'] = $user_id;
                    $bind_data['access_token'] = $qc_info['access_token'];
                    $bind_data['open_id'] = $qc_info['open_id'];
                    $bind_data['nickname'] = $qc_info['nickname'];
                    $bind_data['sex'] = $qc_info['gender'];
                    $bind_data['province'] = $qc_info['province'];
                    $bind_data['city'] = $qc_info['city'];
                    $bind_data['head_img_s'] = $qc_info['head_img_s'];
                    $bind_data['head_img_b'] = $qc_info['head_img_b'];
                    $bind_data['create_time'] = time();
                    $this->users_model->insert_qq_bind($bind_data);
                    
                }elseif($type == 2){//微博快捷绑定登录
                    $bind_data['user_id'] = $user_id;
                    $bind_data['access_token'] = $qc_info['access_token'];
                    $bind_data['uid'] = $qc_info['open_id'];
                    $bind_data['nickname'] = $qc_info['nickname'];
                    $bind_data['sex'] = $qc_info['gender'];
                    $bind_data['province'] = $qc_info['province'];
                    $bind_data['city'] = $qc_info['city'];
                    $bind_data['head_img_s'] = $qc_info['head_img_s'];
                    $bind_data['head_img_b'] = $qc_info['head_img_b'];
                    $bind_data['create_time'] = time();
                    $this->users_model->insert_qq_bind($bind_data);
                }
                
                //推荐人信息
                if($parent_id){
                    $spread_data = array('user_id'=>$user_id,'parent_id'=>$parent_id,'create_time'=>  time(),'month'=>$month,'day'=>$day);
                    $this->users_spread_model->insert($spread_data);
                    //插入推广记录
                }
                //用户附加信息
                $info_data = array();
                $info_data['user_id'] = $user_id;
                $info_data['nickname'] = $qc_info['nickname'];
                $info_data['sex'] = $qc_info['gender'];
                $info_data['head_img'] = "";
                $info_data['head_small_img'] = "";
                $info_data['province'] = $qc_info['province'];
                $info_data['city'] = $qc_info['city'];
                $this->users_model->insert_user_info($info_data);//插入用户附加信息
                //用户账户信息
                $account_data = array();
                $account_data['user_id'] = $user_id;
                $account_data['total'] = 0;//账户总额
                $account_data['balance'] = 0;//可用余额
                $account_data['experience'] = 0;//当前积分
                $account_data['experience_balance'] = 0;//可用积分
                $this->account_model->insert($account_data);//插入用户账号信息
                //新手任务
                $new_user_task = array();
                $new_user_task['user_id'] = $user_id;
                $new_user_task['step'] = 1;
                $new_user_task['reward_worth'] = $this->new_task[0]['award_finance'];
                $new_user_task['reward_experience'] = $this->new_task[0]['award_experience'];
                $new_user_task['is_finish'] = 1;
                $new_user_task['is_receive'] = 0;
                $new_user_task['create_time'] = time();
                $new_user_task['finish_time'] = time();
                $this->new_task_model->insert($new_user_task);//新手任务第一步
                
                $new_user_task['step'] = 2;
                $new_user_task['reward_worth'] = $this->new_task[1]['award_finance'];
                $new_user_task['reward_experience'] = $this->new_task[1]['award_experience'];
                $new_user_task['is_finish'] = 0;
                $new_user_task['finish_time'] = 0;
                $this->new_task_model->insert($new_user_task);//新手任务第二步
                
                $new_user_task['step'] = 3;
                $new_user_task['is_finish'] = 0;
                $new_user_task['finish_time'] = 0;
                $new_user_task['reward_worth'] = $this->new_task[2]['award_finance'];
                $new_user_task['reward_experience'] = $this->new_task[2]['award_experience'];
                $this->new_task_model->insert($new_user_task);//新手任务第三步
                //奖励通知
                $message = "恭喜您完成新手任务第一步【".$this->new_task[0]['title']."】,奖励您<span class='mail-award'>".$this->new_task[0]['award_finance']."溜溜币</span>和<span class='mail-award'>".$this->new_task[0]['award_experience']."积分</span>，赶快去<a href='/mtask.html' target='_blank' class='mail-award-a'>领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                $this->db->insert("users_award_notice", array('user_id'=>$user_id,'title'=>'任务通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));
                /*
                //资金明细
                $finance_data = array();
                $finance_data['user_id'] = $user_id;
                $finance_data['content'] = '新人注册奖励礼包，'.$reg_finance_award.'溜溜币';
                $finance_data['finance'] = $reg_finance_award;
                $finance_data['finance_balance'] = $account_data['balance'];
                $finance_data['reward_type'] = 'reg_award';
                $finance_data['type'] = 1;
                $finance_data['create_time'] = time();
                $finance_data['month'] = $month;
                $finance_data['day'] = $day;
                $this->finance_list_model->insert($finance_data);
                        
                //积分明细
                $experience_data = array();
                $experience_data['user_id'] = $user_id;
                $experience_data['content'] = '新用户注册奖励'.$reg_experience_award.'积分值';
                $experience_data['experience'] = $reg_experience_award;
                $experience_data['experience_balance'] = $account_data['experience_balance'];
                $experience_data['reward_type'] = 'reg_award';
                $experience_data['type'] = 1;
                $experience_data['create_time'] = time();
                $experience_data['month'] = $month;
                $experience_data['day'] = $day;
                $this->experience_list_model->insert($experience_data);
                */
                //用户日志
                $log_data = array();
                $log_data['user_id'] = $user_id;
                $log_data['events'] = 'register';
                $log_data['content'] = '注册成功';
                $log_data['ip'] = $this->input->ip_address();;
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                //自动登录
                $info = array();
                $info['user_id'] = $user_id;
                $info['username'] = $data['username'];
                $info['mobile'] = $data['mobile'];
                $info['email'] = $data['email'];
                $info['last_ip'] = $data['last_ip'];
                $info['last_time'] = $data['last_time'];
                $this->session->set_userdata('userinfo', $info);
                
                $mess['info'] = '注册成功';
                $mess['status'] = 0;
                $mess['url'] = site_url('user');
                echo JSON($mess);exit;
                
            }else{
                $mess['info'] = '注册失败';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
        }
    }
    
    //已有账号绑定快捷登录
    function old_bind(){
        if($this->userinfo){
            redirect('user');
        }
        $mess = array();
        if($_POST){
            $uname = trim($_POST['uname']);
            $upass = trim($_POST['upass']);
            $type = trim($_POST['type']);
            $qc_info = json_decode(base64_decode(trim($_POST['qc_uinfo'])),true);
            //$ucaptcha = trim($_POST['ucaptcha']);
            if(!$uname){
                $mess['info'] = '用户名不能为空';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            if(!$upass){
                $mess['info'] = '密码不能为空';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            /*
            if(!$ucaptcha){
                $mess['info'] = '验证码不能为空';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }*/
            $uinfo = 0;
            if(check_email($uname)){
                $uinfo = $this->users_model->get_user_by_email($uname);
            }elseif(check_mobile($uname)){
                $uinfo = $this->users_model->get_user_by_mobile($uname);
            }else{
                $uinfo = $this->users_model->get_user_by_username($uname);
            }
            //验证账号
            if(!$uinfo){
                $mess['info'] = '用登录账号输入有误';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            //查看是否被锁定
            if($uinfo['status'] == 0){
                $mess['info'] = '对不起，您的账户处于锁定状态';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            $ip = $this->input->ip_address();
            
            $is_pass = $uinfo['password'] == md5(md5($upass) . $uinfo['salt']);
            
            //验证密码
            if(!$is_pass){
                $mess['info'] = '密码错误';
                $mess['status'] = '1';
                $mess['url'] = '';
                echo JSON($mess);exit;
            }
            
            
            
            if($type == 1){//qq快捷登录
                
                $bind = $this->users_model->get_qq_bind(array('user_id'=>$uinfo['user_id']));
                if($bind){
                    $mess['info'] = "<i style='font-size:12px;margin-top:10px;'>该账号已经绑定过QQ<i style='color:red;'>‘".$bind['nickname']."’</i>账号<br/><br/>不能重复绑定！</i>";
                    $mess["status"] = '1';
                    $mess['url'] = '';
                    echo JSON($mess);exit;
                }
                $bind_data['user_id'] = $uinfo['user_id'];
                $bind_data['access_token'] = $qc_info['access_token'];
                $bind_data['open_id'] = $qc_info['open_id'];
                $bind_data['nickname'] = $qc_info['nickname'];
                $bind_data['sex'] = $qc_info['gender'];
                $bind_data['province'] = $qc_info['province'];
                $bind_data['city'] = $qc_info['city'];
                $bind_data['head_img_s'] = $qc_info['head_img_s'];
                $bind_data['head_img_b'] = $qc_info['head_img_b'];
                $bind_data['create_time'] = time();
                $this->users_model->insert_qq_bind($bind_data);
            }elseif($type == 2){//微博快捷登录
                $bind = $this->users_model->get_weibo_bind(array('user_id'=>$uinfo['user_id']));
                if($bind){
                    $mess['info'] = "<i style='font-size:12px;margin-top:10px;'>该账号已经绑定过微博<i style='color:red;'>‘".$bind['nickname']."’</i>账号<br/><br/>不能重复绑定！</i>";
                    $mess["status"] = '1';
                    $mess['url'] = '';
                    echo JSON($mess);exit;
                }
                $bind_data['user_id'] = $uinfo['user_id'];
                $bind_data['access_token'] = $qc_info['access_token'];
                $bind_data['uid'] = $qc_info['open_id'];
                $bind_data['nickname'] = $qc_info['nickname'];
                $bind_data['sex'] = $qc_info['gender'];
                $bind_data['province'] = $qc_info['province'];
                $bind_data['city'] = $qc_info['city'];
                $bind_data['head_img_s'] = $qc_info['head_img_s'];
                $bind_data['head_img_b'] = $qc_info['head_img_b'];
                $bind_data['create_time'] = time();
                $this->users_model->insert_weibo_bind($bind_data);
            }
            
            //更新账号信息
            $user_info = array();
            $user_info['nickname'] = $qc_info['nickname'];
            $user_info['sex'] = $qc_info['gender'];
            $user_info['province'] = $qc_info['province'];
            $user_info['city'] = $qc_info['city'];
            $this->users_model->user_info_update($user_info,$uinfo['user_id']);
            
            //登录成功，写入日志记录
            $log_data = array();
            $log_data['user_id'] = $uinfo['user_id'];
            $log_data['events'] = 'login';
            $log_data['content'] = '快捷登录成功';
            $log_data['ip'] = $this->input->ip_address();
            $log_data['create_time'] = time();
            $this->users_log_model->insert($log_data);
            //更改用户信息
            $data = array();
            $data['login_times'] = $uinfo['login_times'] + 1;
            $data['last_ip'] = $uinfo['login_ip'];
            $data['last_time'] = $uinfo['login_time'];
            $data['login_ip'] = $this->input->ip_address();
            $data['login_time'] = time();
            $this->users_model->user_update($data,$uinfo['user_id']);
            //登录
            $info = array();
            $info['user_id'] = $uinfo['user_id'];
            $info['username'] = $data['username'];
            $info['mobile'] = $data['mobile'];
            $info['email'] = $data['email'];
            $info['last_ip'] = $data['last_ip'];
            $info['last_time'] = $data['last_time'];
            $this->session->set_userdata('userinfo', $info);
            
            $mess['info'] = '绑定成功';
            $mess['status'] = 0;
            $mess['url'] = site_url('user');
            echo JSON($mess);exit;
        }
    }
}
